package com.eeepay.eeepay_v2.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.eeepay.eeepay_v2.R;
import com.eeepay.eeepay_v2.model.MerchantQueryCri;
import com.eeepay.eeepay_v2.model.UserInfo;
import com.eeepay.eeepay_v2.util.Constant;
import com.eeepay.eeepay_v2.util.TimeUtils;
import com.eeepay.v2_library.ui.ABBaseActivity;
import com.eeepay.v2_library.view.HorizontalItemView;
import com.eeepay.v2_library.view.LabelEditText;
import com.eeepay.v2_library.view.TitleBar;

/**
 * Created by zw on 2016/3/1 0001.
 * 商户超级查询
 */
public class MerchantQueryActivity extends ABBaseActivity implements View.OnClickListener {

    private TitleBar titleBar;
    private LabelEditText phoneLet;//商户手机号
    private TextView[] time_select = new TextView[3];//时间选择框
    private HorizontalItemView agent_all, device_all, area_all, merchant_state_all, no_device_all;
    private TextView start_time, end_time;//开始时间和结束时间
    private EditText minMoney, maxMoney;//交易金额
    private EditText minTradeCount, maxTradeCount;//交易笔数
    private EditText into_startDate, into_endDate;//进件时间
    private Button btn_confirm;//确定

    private String areaStr;//地区
    private String hpId = "";
    private String noHpId = "";
    private String agentNode = "";//代理商节点
    private String statusCode = "";//商户状态

    @Override
    protected int getLayoutId() {
        return R.layout.activity_merchant_query;
    }

    @Override
    protected void initWidget() {
        titleBar = getViewById(R.id.title_bar);
        titleBar.setShowRight(View.VISIBLE);
        titleBar.setRightTextView("重置");
        titleBar.setRightTextColor(R.color.unify_grounding_white);

        time_select[0] = getViewById(R.id.tv_select_1);
        time_select[1] = getViewById(R.id.tv_select_2);
        time_select[2] = getViewById(R.id.tv_select_3);

        phoneLet = getViewById(R.id.let_merchant_query_phone);
        agent_all = getViewById(R.id.tv_agent_all);
        device_all = getViewById(R.id.tv_device_all);
        area_all = getViewById(R.id.tv_area_all);
        merchant_state_all = getViewById(R.id.hv_merchant_state_all);
        no_device_all = getViewById(R.id.tv_no_device_all);
        //开始时间
        start_time = getViewById(R.id.start_time);
        //结束时间
        end_time = getViewById(R.id.end_time);
        //交易金额
        LinearLayout lLayout_money = getViewById(R.id.money);
        minMoney = (EditText) lLayout_money.findViewById(R.id.input_1);
        maxMoney = (EditText) lLayout_money.findViewById(R.id.input_2);
        //交易笔数
        LinearLayout lLayout_count = getViewById(R.id.trade_count);
        minTradeCount = (EditText) lLayout_count.findViewById(R.id.input_1);
        maxTradeCount = (EditText) lLayout_count.findViewById(R.id.input_2);
        //进件时间
        LinearLayout lLayout_into_time = getViewById(R.id.into_date);
        into_startDate = (EditText) lLayout_into_time.findViewById(R.id.input_1);
        into_startDate.setFocusableInTouchMode(false);
        into_endDate = (EditText) lLayout_into_time.findViewById(R.id.input_2);
        into_endDate.setFocusableInTouchMode(false);

        btn_confirm = getViewById(R.id.btn_confirm);
    }


    @Override
    protected void eventOnClick() {
        //重置
        titleBar.setRightOnClickListener(new TitleBar.RightBtnOnClickListener() {
            @Override
            public void onRightClick(View view) {
                reset();
            }
        });
        for (TextView tv : time_select) {
            tv.setOnClickListener(this);
        }
        agent_all.setOnClickListener(this);
        device_all.setOnClickListener(this);
        area_all.setOnClickListener(this);
        merchant_state_all.setOnClickListener(this);
        no_device_all.setOnClickListener(this);

        TimeUtils.newDateTimePicker(mContext, start_time, TimeUtils.DATE_TIME);
        TimeUtils.newDateTimePicker(mContext, end_time, TimeUtils.DATE_TIME);
        TimeUtils.newDateTimePicker(mContext,into_startDate,TimeUtils.DATE);
        TimeUtils.newDateTimePicker(mContext,into_endDate,TimeUtils.DATE);
//        TimeUtils.selectTime(mContext, into_startDate, TimeUtils.DATE);
//        TimeUtils.selectTime(mContext, into_endDate, TimeUtils.DATE);

        btn_confirm.setOnClickListener(this);

    }

    @Override
    protected void sendHttpRequest(int taskId) {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_select_1:
                changeSelected(time_select, 0);
                break;
            case R.id.tv_select_2:
                changeSelected(time_select, 1);
                break;
            case R.id.tv_select_3:
                changeSelected(time_select, 2);
                break;
            case R.id.tv_agent_all://代理商
                goActivityForResult(AgentQueryActivity.class, Constant.RequestCode.agent_all);
                break;
            case R.id.tv_device_all://设备类型
                goActivityForResult(DeviceTypeActivity.class, Constant.RequestCode.device_all);
                break;
            case R.id.tv_area_all://商户地区
                goActivityForResult(MerchantAreaActivity.class, Constant.RequestCode.area_all);
                break;
            case R.id.hv_merchant_state_all://商户状态
                goActivityForResult(MerchantStateActivity.class, Constant.RequestCode.merchant_state_all);
                break;
            case R.id.tv_no_device_all:
                goActivityForResult(DeviceTypeActivity.class, Constant.RequestCode.no_device_all);
                break;
            case R.id.btn_confirm:
                queryResult();
                break;
        }
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode != RESULT_OK) {
            return;
        }
        switch (requestCode) {
            case Constant.RequestCode.agent_all:
                String agentName = data.getStringExtra("agentName");
                agentNode = data.getStringExtra("agentNode");
                agent_all.setRightText(agentName);
                break;
            case Constant.RequestCode.device_all:
                boolean isAll = data.getBooleanExtra("isAll", false);
                if (isAll) {
                    this.device_all.setRightText("全部");
                } else {
                    String device = data.getStringExtra("device_type");
                    if(TextUtils.isEmpty(device)){
                        this.device_all.setRightText("全部");
                        hpId = "";
                    }else {
                        hpId = data.getStringExtra("hp_id");
                        this.device_all.setRightText(device);
                    }
                }
                break;
            case Constant.RequestCode.merchant_state_all:
                boolean isAll2 = data.getBooleanExtra("isAll", false);
                if (isAll2) {
                    this.merchant_state_all.setRightText("全部");
                    statusCode = "";
                } else {
                    String state = data.getStringExtra("state");
                    statusCode = data.getStringExtra("stateCode");
                    if(TextUtils.isEmpty(state)){
                        this.merchant_state_all.setRightText("全部");
                    }else {
                        this.merchant_state_all.setRightText(state);
                    }

                }
                break;
            case Constant.RequestCode.area_all:
                areaStr = data.getStringExtra("area");
                area_all.setRightText(areaStr);
                break;
            case Constant.RequestCode.no_device_all:
                boolean isAll3 = data.getBooleanExtra("isAll", false);
                if (isAll3) {
                    this.no_device_all.setRightText("全部");
                } else {
                    String device = data.getStringExtra("device_type");
                    if(TextUtils.isEmpty(device)){
                        this.no_device_all.setRightText("全部");
                        noHpId = "";
                    }else {
                        noHpId = data.getStringExtra("hp_id");
                        this.no_device_all.setRightText(device);
                    }
                }
                break;
        }
    }

    /**
     * 统计查询条件，跳转结果页面
     */
    private void queryResult() {
        MerchantQueryCri merchantQueryCri = new MerchantQueryCri();
        if (!"全部".equals(agent_all.getRightText())) {
            merchantQueryCri.setAgentName(agent_all.getRightText());
            merchantQueryCri.setAgentNode(agentNode);
        }else {
            merchantQueryCri.setAgentName(UserInfo.getUserInfo2SP().getAgentName());
            merchantQueryCri.setAgentNode(UserInfo.getUserInfo2SP().getAgentNode());
        }
        if (!"全部".equals(device_all.getRightText())) {
            merchantQueryCri.setDeviceName(hpId);
        }else {
            merchantQueryCri.setDeviceName("");
        }
        if (!"全部".equals(no_device_all.getRightText())) {
            merchantQueryCri.setNoOpenDevice(noHpId);
        }else {
            merchantQueryCri.setNoOpenDevice("");
        }
        merchantQueryCri.setTradeStartTime(getTextContent(R.id.start_time));
        merchantQueryCri.setTradeEndTime(getTextContent(R.id.end_time));
        merchantQueryCri.setTradeMinMoney(minMoney.getText().toString().trim());
        merchantQueryCri.setTradeMaxMoney(maxMoney.getText().toString().trim());
        merchantQueryCri.setPhone(phoneLet.getEditContent());
        merchantQueryCri.setIntoStartTime(into_startDate.getText().toString().trim());
        merchantQueryCri.setIntoEndTime(into_endDate.getText().toString().trim());
        merchantQueryCri.setTradeStartTime(start_time.getText().toString().trim());
        merchantQueryCri.setTradeEndTime(end_time.getText().toString().trim());
        merchantQueryCri.setTradeMinCount(minTradeCount.getText().toString().trim());
        merchantQueryCri.setTradeMaxCount(maxTradeCount.getText().toString().trim());
        if (!"全部".equals(area_all.getRightText())) {
            merchantQueryCri.setMerchantArea(area_all.getRightText());
            String[] split = area_all.getRightText().split("-");
            merchantQueryCri.setProvince(split[0]);
            merchantQueryCri.setCity(split[1]);
            merchantQueryCri.setDistrict(split[2]);
        }else {
            merchantQueryCri.setMerchantArea("");
            merchantQueryCri.setProvince("");
            merchantQueryCri.setCity("");
            merchantQueryCri.setDistrict("");
        }
        merchantQueryCri.setMerchantStatus(statusCode);

        Bundle bundle = new Bundle();
        bundle.putString(Constant.INTENT_FLAG, Constant.MERCHANT_QUERY);
        bundle.putSerializable(Constant.MERCHANT_QUERY_CRI, merchantQueryCri);
        goActivity(QueryResultActivity.class, bundle);
    }

    /**
     * 改变选择框的选中状态
     *
     * @param num
     */
    private void changeSelected(TextView[] select, int num) {
        for (int i = 0; i < select.length; i++) {
            if (i == num) {
                if (select[i].isSelected()) {
                    select[i].setSelected(false);
                } else {
                    select[i].setSelected(true);
                }
            } else {
                select[i].setSelected(false);
            }
        }
    }

    /**
     * 重置
     */
    private void reset() {
        //重置所有输入框
        setEmpty(start_time, end_time,
                minMoney, maxMoney, minTradeCount, maxTradeCount, into_startDate, into_endDate,
                phoneLet.getEditText(), agent_all.getRightTv(), device_all.getRightTv(),
                agent_all.getRightTv());
        agent_all.setRightText("全部");
        device_all.setRightText("全部");
        hpId = "";
        noHpId = "";
        agentNode = "";
        statusCode = "";
        area_all.setRightText("全部");
        no_device_all.setRightText("全部");
        merchant_state_all.setRightText("全部");
    }

    /**
     * 输入框置空
     * @param tvs
     */
    private void setEmpty(TextView... tvs) {
        for (TextView tv : tvs) {
            tv.setText("");
        }
    }
}
